package com.art.sunflower.controller;

import com.art.sunflower.pojo.Classroom;
import com.art.sunflower.service.ClassroomService;
import com.art.sunflower.service.StudentService;
import com.art.sunflower.service.TeacherService;
import com.art.sunflower.service.UserService;
import com.art.sunflower.util.Message;
import com.art.sunflower.util.UploadUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("Upload")
public class UploadController {
    @Resource
    UserService userService;

    @Resource
    ClassroomService classroomService;

    @Resource
    TeacherService teacherService;

    @Resource
    StudentService studentService;

//    @ApiOperation("批量导入学生")
//    @PostMapping("/studentImportExcel")
//    public Message studentImportExcel(@RequestParam("file") MultipartFile file) throws IOException, Exception {
//        UploadUtils uploadUser = new UploadUtils();
//        int result = 0;
//        List<StudentExample> studentList = uploadUser.studentImportExcel(file);
//        List<StudentExample> students = studentService.selectAllStudent();
//        for (int i = 0; i < 4; i++) {
//            StudentExample studentExample = students.get(i);
//            for (int j = 0; j < 4; j++) {
//                StudentExample studentOne = studentList.get(j);
//                if (studentExample.getUsername() == studentOne.getUsername()) {
//                    studentList.remove(j);
//                }
//            }
//        }
//        for (int i = 5; i < studentList.size(); i++) {
//            StudentExample sReal = studentList.get(i);
//            sReal.setUser_id(Integer.valueOf(studentService.getId()));
//            result = studentService.insert(sReal);   //批量添加 (执行sql语句批量增加)
//        }
//        if (result >= 1) {
//            return Message.createSuc("数据导入成功,并排除部分重复数据");
//        } else
//            return Message.createErr("数据导入异常");
//    }

    //
//    @ApiOperation("批量导入用户")
//    @ResponseBody
//    @PostMapping("/UserImportExcel")
//    public Message teacherImportExcel(@RequestParam("file") MultipartFile file) throws IOException, Exception {
//        UploadUtils uploadUser = new UploadUtils();
//        int result = 0;
//        List<User> userList = uploadUser.userImportExcel(file);
//        List<User> users = userService.selectAll();
//        for (int i = 0; i < users.size(); i++) {
//            User user = users.get(i);
//            for (int j = 0; j < userList.size(); j++) {
//                User userOne = userList.get(j);
//                if (user.getUsername() == userOne.getUsername()) {
//                    userList.remove(j);
//                }
//            }
//        }
//        for (int i = 0; i < userList.size(); i++) {
//            User tReal = userList.get(i);
//            if (tReal.getUsername().equals("") || tReal.getUsername().equals(""))
//                return Message.createErr("教师工号不能为空");
//            else if (tReal.getUsername().length() > 10)
//                return Message.createErr("工号长度不能超过10");
//            result = userService.insertUser(tReal);   //批量添加 (执行sql语句批量增加)
//        }
//        if (result >= 1) {
//            return Message.createSuc("数据导入成功,并排除部分重复数据");
//        } else
//            return Message.createErr("数据导入异常");
//    }
//
    @RequiresRoles(value = {"admin"})
    @ApiOperation("批量导入班级")
    @PostMapping("/classImportExcel")
    public Message classImportExcel(MultipartFile file) throws IOException, Exception {
        UploadUtils uploadUser = new UploadUtils();
        int result = 0;
        List<Classroom> classroomList = uploadUser.classImportExcel(file);
        List<Classroom> classrooms = classroomService.selectAll();
        for (Classroom classroom : classrooms) {
            for (int j = 0; j < classroomList.size(); j++) {
                Classroom classroomOne = classroomList.get(j);
                if (classroom.getRoomName().equals(classroomOne.getRoomName())) {
                    classroomList.remove(j);
                }
            }
        }
        for (Classroom cReal : classroomList) {
            if (teacherService.selectOneTeacherById(cReal.getTeacherId()) != null)
                cReal.setTeacherId(teacherService.selectOneTeacherById(cReal.getTeacherId()).getId());
            else if (cReal.getRoomName().equals(""))
                return Message.createErr("班级名称不能为空");
            result = classroomService.insert(cReal);   //批量添加 (执行sql语句批量增加)
        }
        if (result >= 1) {
            return Message.createSuc("数据导入成功,并排除部分重复数据");
        } else
            return Message.createErr("数据导入异常");
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation("下载学生导入模板")
    @ResponseBody
    @GetMapping("getStudentExcel")
    public Message getStudentExcel() {
        return Message.createSuc("http://120.27.233.52:8080/student.xlsx");
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation("下载教师导入模板")
    @ResponseBody
    @GetMapping("getTeacherExcel")
    public Message getTeacherExcel() {
        return Message.createSuc("http://120.27.233.52:8080/teacher.xlsx");
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation("下载班级导入模板")
    @ResponseBody
    @GetMapping("getClassExcel")
    public Message getClassExcel() {
        return Message.createSuc("http://120.27.233.52:8080/classroom.xlsx");
    }
}

